<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" size="small">
      <el-form-item>
        <el-input v-model="dataForm.deName" placeholder="请输入配送员姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchDataList()" size="small">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()" size="small">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="index"
        header-align="center"
        align="center"
        width="80"
        label="序号">
      </el-table-column>
      <el-table-column
        prop="deName"
        header-align="center"
        align="center"
        width="120"
        label="配送员姓名">
      </el-table-column>
      <el-table-column
        prop="shopName"
        header-align="center"
        align="center"
        label="商铺名称">
      </el-table-column>
      <el-table-column
        prop="dePhone"
        header-align="center"
        align="center"
        label="电话号码">
      </el-table-column>
      <el-table-column
        prop="serviceValueName"
        header-align="center"
        align="center"
        width="120"
        label="服务类目">
        <template slot-scope="scope">
          <el-tag >{{scope.row.serviceValueName}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="deBank"
        header-align="center"
        align="center"
        label="开户行">
      </el-table-column>
      <el-table-column
        prop="deBankCard"
        header-align="center"
        align="center"
        width="180"
        label="银行卡号">
      </el-table-column>
      <el-table-column
        prop="totalCommission"
        header-align="center"
        align="center"
        width="120"
        label="配送费总额">
      </el-table-column>
      <el-table-column
        prop="noSettlementMoney"
        header-align="center"
        align="center"
        width="120"
        label="未结算金额">
      </el-table-column>
      <el-table-column
        prop="canDrawMoney"
        header-align="center"
        align="center"
        width="120"
        label="可提现金额">
      </el-table-column>
      <el-table-column
        prop="drawedMoney"
        header-align="center"
        align="center"
        width="120"
        label="已提现金额">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          <el-button type="text" size="small" v-if="scope.row.checkStatus == 0 || scope.row.checkStatus == 2" @click="handleOpenDialogAudit(scope.row)">审核</el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="currentChangeHandle"
      :current-page="page.currentPage"
      :page-size="page.pageSize"
      :total="page.total"
      layout="total, prev, pager, next">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList(page,searchForm)"></add-or-update>

    <el-dialog
      title="提示"
      :before-close="handlerCancelAuditDialog"
      :visible.sync="auditManDialogVisible"
      width="40%">
      <span>是否通过该配送员</span>
      <div style="margin-top: 15px">
        <el-radio-group v-model="checkStatus">
          <el-radio :label="2">驳回</el-radio>
          <el-radio :label="1">通过</el-radio>
        </el-radio-group>
      </div>
      <el-input
        style="margin-top: 15px"
        v-if="checkStatus==2"
        type="textarea"
        :autosize="{ minRows: 2, maxRows: 4}"
        placeholder="请输入驳回原因"
        maxlength="50"
        v-model="reason">
      </el-input>
      <span slot="footer" class="dialog-footer">
    <el-button @click="handlerCancelAuditDialog" size="small">取消</el-button>
    <el-button type="primary" @click="handlerAudit(1)" size="small">确定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  import AddOrUpdate from './deliveryman-add-or-update'
  import {addObj, deliveryCheck, delObj, fetchList, getObj, putObj, search} from '@/api/delivery/deliveryman'
  export default {
    data () {
      return {
        dataForm: {
          deName: ''
        },
        checkStatus: 1,
        reason: '',
        dataList: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        dataListLoading: false,
        auditManDialogVisible: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        searchForm: {
          shopId: this.$store.state.user.shopId,
          deName: '',
          dePhone: '',
          checkStatus: ''
        },
        form: {}
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      // this.getDataList()
    },
    methods: {
      init(params) {
        if (params) {
          this.searchForm.checkStatus = params.checkStatus
        }
        if (!params) {
          params = {
            shopId: ''
          }
        }
        if (this.searchForm.shopId) {
          params.shopId = this.searchForm.shopId
        }
        this.getDataList(this.page, params)
      },
      // 获取数据列表
      getDataList (page, params) {
        this.dataListLoading = true
        fetchList(Object.assign({
          current: page.currentPage,
          size: page.pageSize,
          deName: this.dataForm.deName
        }, params))
          .then(res => {
            if (res.data && res.data.code === 0) {
              this.dataList = res.data.data.records
              this.page.total = res.data.data.total
            } else {
              this.dataList = []
              this.page.total = 0
            }
            this.dataListLoading = false
          })
      },
      // 当前页
      currentChangeHandle (val) {
        this.page.currentPage = val
        this.getDataList(this.page, this.searchForm)
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        // var ids = id ? [id] : this.dataListSelections.map(item => {
        //   return item.id
        // })
        this.$confirm(`确定对[id=${id}]进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delObj(id)
            .then(res => {
              if (res.data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                })
                this.getDataList(this.page, this.searchForm)
              } else {
                this.$message.error(data.msg)
              }
            })
        }).catch(err => {

        })
      },
      //打开审核对话框
      handleOpenDialogAudit(row) {
        this.form = row
        this.auditManDialogVisible = true
      },
      //审核
      handlerAudit() {
        const data = new FormData()
        data.append('userId', this.form.userId)
        data.append('deId', this.form.id)
        data.append('checkStatus', this.checkStatus)
        if (this.checkStatus == 2) {
          if (!this.reason){
            this.$message.error('请输入驳回原因')
          }
          data.append('reason', this.reason)
        }
        deliveryCheck(data).then(res => {
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'success'
          })
          this.auditManDialogVisible = false
          this.getDataList(this.page, this.searchForm)
          this.handlerCancelAuditDialog()
        })
      },
      searchDataList() {
        this.page.currentPage = 1
        this.getDataList(this.page, {
          'deName': this.dataForm.deName
        })
      },
      //取消对话框
      handlerCancelAuditDialog() {
        this.auditManDialogVisible = false
        this.checkStatus = 1
        this.reason = ''
      }
    }
  }
</script>
